package controller;

import common.pojo.CommonResult;
import common.spring.redis.RedisUnits;
import common.utils.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.WxUserInfo;
import pojo.form.WxUserInfoAddForm;
import pojo.form.WxUserInfoQueryForm;
import service.WxUserInfoService;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by asus on 2018/10/19.
 */
@Controller
@RequestMapping("/info")
public class WxUserInfoController {
    private final Logger LOG = LoggerFactory.getLogger(WxUserInfoController.class);

    @Autowired
    WxUserInfoService infoService;

    @Autowired
    RedisUnits redisUnits;


    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<WxUserInfo> getWxUserInfo(
            @RequestParam(value = "sessionId", required = true) String sessionId
    ) {
        if (StringUtils.isBlank(sessionId)) {
            return CommonResult.returnFailsWrapper("请传入sessionID");
        }
        Map map = redisUnits.getMap(sessionId);
        if(map==null|| map.size()==0){
            return new CommonResult(3,"");
        }
        LOG.warn("-getWxUserInfo-MAP-"+ JsonUtils.toJson(map));
        WxUserInfo wxUserInfo = infoService.getWxUserInfo(map.get("openId").toString());
        if(wxUserInfo==null){
            LOG.warn("-getWxUserInfo-null-"+ "身份信息失效，请重新登录");
            return CommonResult.returnFailsWrapper("身份信息失效，请重新登录");
        }
        return CommonResult.returnDataWrapper(wxUserInfo);
    }


    @RequestMapping(value = "/bossWxId",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getWxUser(
            @RequestParam(value = "bossWxId", required = false) String bossWxId
    ){
        if (StringUtils.isBlank(bossWxId)) {
            return CommonResult.returnFailsWrapper("请传入bossWxId");
        }
        WxUserInfo wxUserInfo = infoService.getWxUserInfo(bossWxId);
        return CommonResult.returnDataWrapper(wxUserInfo).setTransientData(wxUserInfo);
    }


    @RequestMapping(value = "/id",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getWxUserId(
            @RequestParam(value = "id", required = false) String id
    ){
        if (StringUtils.isBlank(id)) {
            return CommonResult.returnFailsWrapper("请传入id");
        }
        WxUserInfo wxUserInfo = infoService.getWxUserInfo(NumberUtils.toLong(id));
        return CommonResult.returnDataWrapper(wxUserInfo);
    }


    @ResponseBody
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public CommonResult updateWxUserInfo(
            @RequestParam(value = "openId", required = false) String openId
    ){
        if (StringUtils.isBlank(openId)) {
            return CommonResult.returnFailsWrapper("请传入openId");
        }
        return CommonResult.returnBoolWrapper(infoService.updateWxUserInfo(openId)>0,"修改失败");
    }


    @ResponseBody
    @RequestMapping(value = "/qrcode",method = RequestMethod.POST)
    public CommonResult updateWxUserInfo(
           HttpServletRequest request
    ){
        String sessionId = request.getHeader("sessionId");
        if (StringUtils.isBlank(sessionId)) {
            return new CommonResult(3, "请重新登录");
        }
        Map map = redisUnits.getMap(sessionId);
        if(map==null|| map.size()==0){
            return new CommonResult(3,"");
        }
        LOG.warn("-getWxUserInfo-MAP-"+ JsonUtils.toJson(map));
        WxUserInfo wxUserInfo = infoService.getWxUserInfo(map.get("openId").toString());
        if(wxUserInfo==null){
            LOG.warn("-getWxUserInfo-null-"+ "身份信息失效，请重新登录");
            return CommonResult.returnFailsWrapper("身份信息失效，请重新登录");
        }
        return CommonResult.returnDataWrapper(wxUserInfo.getMineQrCode());

            }

    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public CommonResult editWxUserInfo(
            @RequestParam(value = "openId", required = false) String openId,
            @RequestParam(value = "deptId", required = false) String deptId,
            @RequestParam(value = "agentId", required = false) String agentId
    ){
        if (StringUtils.isBlank(openId)) {
            return CommonResult.returnFailsWrapper("请传入openId");
        }
        if (StringUtils.isBlank(deptId)) {
            return CommonResult.returnFailsWrapper("请传入deptId");
        }
        if (StringUtils.isBlank(agentId)) {
            return CommonResult.returnFailsWrapper("请传入agentId");
        }
        return CommonResult.returnBoolWrapper(infoService.editWxUserInfo(openId,NumberUtils.toLong(deptId),NumberUtils.toLong(agentId))>0,"修改失败");
    }

    @ResponseBody
    @RequestMapping(value = "/edit/agentId",method = RequestMethod.POST)
    public CommonResult editWxUserInfo(
            @RequestParam(value = "openId", required = false) String openId,
            @RequestParam(value = "agentId", required = false) String agentId
    ){
        if (StringUtils.isBlank(openId)) {
            return CommonResult.returnFailsWrapper("请传入openId");
        }
        if (StringUtils.isBlank(agentId)) {
            return CommonResult.returnFailsWrapper("请传入agentId");
        }

        return CommonResult.returnBoolWrapper(infoService.editWxUserInfo(openId,NumberUtils.toLong(agentId))>0,"修改失败");
    }

    @ResponseBody
    @RequestMapping(value = "/edit/deptId",method = RequestMethod.POST)
    public CommonResult editWxUserInfoDeptId(
            @RequestParam(value = "openId", required = false) String openId,
            @RequestParam(value = "deptId", required = false) String deptId
    ){
        if (StringUtils.isBlank(openId)) {
            return CommonResult.returnFailsWrapper("请传入openId");
        }
        if (StringUtils.isBlank(deptId)) {
            return CommonResult.returnFailsWrapper("请传入deptId");
        }

        return CommonResult.returnBoolWrapper(infoService.editWxUserInfoDeptId(openId,NumberUtils.toLong(deptId))>0,"修改失败");
    }

    @ResponseBody
    @RequestMapping(value = "/page",method = RequestMethod.GET)
   public CommonResult getWxUserInfoPage(
           @ModelAttribute WxUserInfoQueryForm queryForm,
           @RequestParam(value = "start", required = false) String start,
             @RequestParam(value = "limit", required = false) String limit,
           @RequestParam(value = "curPage", required = false, defaultValue = "1") String curPage,
           @RequestParam(value = "pageSize", required = false, defaultValue = "50") String pageSize
   ) {
       if (start != null) {
           curPage = String.valueOf(NumberUtils.toInt(start, 0) / NumberUtils.toInt(limit, 10) + 1);
           pageSize = limit;
       }
       return CommonResult.returnDataWrapper(infoService.getWxUserInfo(queryForm,NumberUtils.toInt(curPage),NumberUtils.toInt(pageSize)));
   }
}
